package cn.chainco.cts.iserviceImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import cn.chainco.cts.idao.ImDao;
import cn.chainco.cts.iservice.ImManager;
import cn.chainco.cts.jdo.IM;
import cn.chainco.cts.jdo.User;
import cn.chainco.cts.utils.query.OP;
import cn.chainco.cts.utils.query.QueryOrder;
import cn.chainco.cts.utils.query.QueryParam;

public class ImManagerImpl extends CommonManagerImpl implements ImManager {

private ImDao imDao;
	
	public void setImDao(ImDao imDao) {
		this.imDao = imDao;
	}
	
   /**
   * 查询登入人的未读信息
   */
	public List<IM> getOneUserNotReadInfo( User user){
		return this.imDao.getOneUserNotReadInfo(user);
	}
	
   /**
   * 查询登入人的未读信息且分组
   */
	public Map<User,Integer> getNotReadInfoGroupByUser( User user){
		Map<User,Integer> map = new HashMap<User,Integer>();
		List<IM> list = getOneUserNotReadInfo(user);
		Set<User> set = new HashSet<User>();
		for( IM obj: list ){
			set.add( obj.getSender());
		}
		for( User obj2: set ){
			int count = 0;
			for( IM obj: list ){
				if( obj.getSender().getId() == obj2.getId() ){
					count++;
				}
			}
			map.put( obj2, count );
		}
		return map;
		
	}
		
	 /**
	   * 查询登入人的未读信息且分组且并排序
	   */
		public List<Map<User,Integer>> getListNotReadInfoGroupByUser( User user){
			List<IM> list = getOneUserNotReadInfo(user);
			Set<User> set = new HashSet<User>();
			List<Map<User,Integer>> list2 = new ArrayList<Map<User,Integer>>();
			for( IM obj: list ){
				set.add( obj.getSender());
			}
			for( User obj2: set ){
				Map<User,Integer> map = new HashMap<User,Integer>();
				int count = 0;
				for( IM obj: list ){
					if( obj.getSender().getId() == obj2.getId() ){
						count++;
					}
				}
				map.put( obj2, count );
				list2.add( map );
			}
			return list2;
			
		}
			
   /**
    * 查询某个人对某个人的聊天记录
    */	
	public List<IM> getChatHistory( User user1, User user2,String content  ){
		return this.imDao.getChatHistory(user1, user2,content);
	}
	/**
	 * 查询登入人对某个人的未读信息(user1:收件人，user2:发件人)
	 */
	public List<IM> getNotReadChatByOneUser( User user1, User user2){
		return this.imDao.getNotReadChatByOneUser(user1, user2);
	}
	
	/**
	 * 修改信息读的状态为已读
	 */
	public int modifyIMsReadState( List<IM> list ){
		if( list == null || list.size() == 0 ) return 0;
		int data = 1;
		try {
			for( IM obj: list ){
				obj.setIsRead(1);
				this.imDao.update( obj );
			}
		} catch (Exception e) {
			e.printStackTrace();
			data = 2;
		}
		return data;
	}
	
	/**
    * 查询某个人对某个人的最新的那条聊天记录
    */
	public IM getNewChat( User user1, User user2){
		return this.imDao.getNewChat(user1, user2);
	}
	
	/**
	 * 查某条单的聊天记录
	 */
	public List<IM> getIMByWorkId( long workId,long userId ,String content){
		if( workId == 0 ) return new ArrayList<IM>();
		List<QueryParam> queryParam = new ArrayList<QueryParam>();
		QueryParam param = new QueryParam();
		param.setField("workId");
		param.setOp(OP.equal);
		param.setValue(new Object[]{workId});
		queryParam.add(param);
		
		
		if( userId != 0 ){
			QueryParam param2 = new QueryParam();
			param2.setField("(senderId="+userId+" or receiverId="+userId+")");
			param2.setOp(OP.sql);
			param2.setValue(new Object[]{});
			queryParam.add(param2);
		}
		
		if(content!=null && !"".equals(content)){
			QueryParam param2 = new QueryParam();
			param2.setField("msgContent");
			param2.setOp(OP.like);
			param2.setValue(new Object[]{"%"+content+"%"});
			queryParam.add(param2);
		}
		
		List<QueryOrder> queryOrder = new ArrayList<QueryOrder>();
		QueryOrder order = new QueryOrder();
		order.setField("sendDate");
		order.setAscOrder(true);
		queryOrder.add(order);
		
		return this.imDao.findByCustomized( IM.class, queryParam, queryOrder);
	}
	
	/**
	 * 查询最近联系人的聊天记录
	 */
	public List<User> getNearIMByUser( User sender){
		List<User> list3 = new ArrayList<User>();
		
		List<IM> listIm = this.imDao.getNearIMByUser(sender);
		List<Long> list1 = new ArrayList<Long>();
		for( IM obj: listIm ){
			try{
				long id=obj.getSender().getId();
			}catch (Exception e) {
				User u;
				u=new User();
				u.setId(0);
				u.setUserName("该用户已被删");
				obj.setSender(u);
			}
			try{
				long id=obj.getReceiver().getId();
			}catch (Exception e) {
				User u;
				u=new User();
				u.setId(0);
				u.setUserName("该用户已被删");
				obj.setReceiver(u);
			}
		}
		for( IM obj: listIm ){
			if( list1.contains( obj.getSender().getId())){
				continue;
			}else{
				list1.add( obj.getSender().getId());
			}
		}
		for( IM obj: listIm ){
			if( list1.contains( obj.getReceiver().getId())){
				continue;
			}else{
				list1.add( obj.getReceiver().getId());
			}
		}
		list3 = new ArrayList<User>();
		for( Long obj: list1 ){
			User user = this.imDao.findByPK( User.class, obj );
			list3.add( user );
			
		}
		
		return list3;
	}
	
	/**
	 * 根据用户查询统计和自己聊过天的人
	 */
	public Set<User> getIMHistoryByUser( User sender){
		List<IM> listIm = this.imDao.getIMHistoryByUser(sender);
		Set<User> set = new HashSet<User>();
		for( IM obj: listIm ){
			set.add( obj.getSender());
			set.add( obj.getReceiver() );
		}
		return set;
	}
	
	/**
	 * 统计某个人和多少单聊过天
	 */
	public List<IM> getIMHistoryGroupByWork( User sender){
		return this.imDao.getIMHistoryGroupByWork(sender);
	}
}
